package com.sniff.atp.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckbs.global.common.utils.DingTalkNoticeUtils;
import com.sniff.atp.modules.test.mapper.BugRegisterMapper;
import com.sniff.atp.modules.test.model.BugRegister;
import com.sniff.atp.modules.test.service.BugRegisterService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 线上问题登记表 服务实现类
 * </p>
 *
 * @author macro
 * @since 2023-08-13
 */
@Service
@Slf4j
public class BugRegisterServiceImpl extends ServiceImpl<BugRegisterMapper, BugRegister> implements BugRegisterService {
    @Override
    public boolean create(BugRegister bugRegister) {
        bugRegister.setCreateTime(new Date());
        bugRegister.setUpdateTime(new Date());


//                产研中心内部群-重要通知
        String robotToken = "04ba85ddf93abb1188c974bf386047bd232e0ac026609c30f02a1010f7a11d51";
        String robotKey = "SEC4dc0f576ee692982a7c390830b3839e536c1cb1e8d0c1186956f2e6daddddfea";


//        String robotToken = "c6c2c7c6c98c2fb5d234319afa9e0dad659152a8518ac739191811ce1bf8a67b";
//        String robotKey = "SECdf56e788259528d23a4b225863526a0bc426c178251313f1e4d5bdc5e9b696d6";

// 定义日期格式
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = outputFormat.format(bugRegister.getBugTime());

        List<String> mobileList = new ArrayList<>();
        String content = ("故障类型：").concat(bugRegister.getBugType()).concat(";\n")
                .concat ("故障描述：【").concat(bugRegister.getLinkProject()).concat("】 ").concat(bugRegister.getBugDescribe()).concat(";\n")
                .concat ("故障原因：").concat(bugRegister.getBugReason()).concat(";\n")
                .concat ("故障等级：").concat(bugRegister.getBugLevel()).concat(";\n")
                .concat ("所属应用：").concat(bugRegister.getBelongApplication()).concat(";\n")
                .concat ("故障跟进人：").concat(bugRegister.getBugFlower()).concat(";\n")
                .concat ("故障时间：").concat(formattedDate).concat(";\n")
                .concat ("故障状态：").concat(bugRegister.getBugStatus()).concat(";");

        DingTalkNoticeUtils.sendDingMsgAll(robotKey,robotToken,mobileList,content);

        return save(bugRegister);
    }

    @Override
    public boolean update(Long id, BugRegister bugRegister) {
        bugRegister.setBugId(id);
        bugRegister.setUpdateTime(new Date());
        return updateById(bugRegister);
    }

    @Override
    public Page<BugRegister> list(BugRegister bugRegister, Integer pageSize, Integer pageNum) {
        Page<BugRegister> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BugRegister> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtil.isNotBlank(bugRegister.getBugDescribe()),BugRegister::getBugDescribe,bugRegister.getBugDescribe())
                .ge(bugRegister.getStartTime() != null,BugRegister::getBugTime,bugRegister.getStartTime())
                .le(bugRegister.getEndTime() != null,BugRegister::getBugTime,bugRegister.getEndTime())
                .eq(StringUtil.isNotBlank(bugRegister.getBugType()),BugRegister::getBugType,bugRegister.getBugType())
                .eq(StringUtil.isNotBlank(bugRegister.getBugStatus()),BugRegister::getBugStatus,bugRegister.getBugStatus())
                .eq(StringUtil.isNotBlank(bugRegister.getBelongApplication()),BugRegister::getBelongApplication,bugRegister.getBelongApplication())
                .eq(StringUtil.isNotBlank(bugRegister.getBugLevel()),BugRegister::getBugLevel,bugRegister.getBugLevel())
                .orderByDesc(BugRegister::getCreateTime);
        return page(page, wrapper);

    }

}
